package com.kchouse.union;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxz.PagerSlidingTabStrip;
import com.kchouse.ListFragment;
import com.kchouse.MainActivity;
import com.kchouse.MineFragment;
import com.kchouse.R;
import com.kchouse.UnionFragment;
import com.kchouse.bean.CashCouponItem;
import com.kchouse.bean.MerchantItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;
import com.kchouse.mine.MineCouponsActivity;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class MerchantDetailsActivity extends BaseActivity implements View.OnClickListener {

    private MerchantDetailsActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private Intent intent;
    private String Merchantid;


    //声明pst的标题
    private ArrayList<Fragment> fragments = new ArrayList<>();
    String[] titles = {"商家介绍","活动","套餐","预存","优惠券"};
//    String[] titles = {"商家介绍","活动列表","优惠券","保障承诺"};
    private PagerSlidingTabStrip pst;
    private ViewPager viewPager;
    private JSONObject merchant;
//    private String protocolListstr;//协议图片数组
    private String suitListstr;//商家套餐列表
    private String couponTemplateListstr;//商家优惠券列表


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_details);

        mContent = this;
        addAct(mContent);
        intiData();
        intiView();
    }

    private void intiData() {

        intent = getIntent();
        Merchantid =  intent.getStringExtra("Merchantid");

        MerchantDetails();
    }


    /**
     * 获取商家详情
     */
    private void MerchantDetails() {

        String url = API.GetMerchantDetails + Merchantid;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
//        params.addHeader("token", MineFragment.token);//添加header信息
//        params.addFormDataPart("token", MineFragment.userid);

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    String merchantstr = data.getString("merchant");
                    merchant = JSONObject.parseObject(merchantstr);
                    String name = merchant.getString("name");
                    tv_title.setText(name);

//                     protocolListstr = data.getString("protocolList");
                    suitListstr = data.getString("suitList");
                    couponTemplateListstr = data.getString("couponTemplateList");


                    intiPSTS();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

//        intiPSTS();
    }

    /**
     * 初始化tab
     */
    private void intiPSTS() {

        pst = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.pager);
        ListFragment list1 = new ListFragment(suitListstr,"联盟活动");
        ListFragment list2 = new ListFragment(couponTemplateListstr,"联盟优惠券");
        MerchantDetailsFragment list3 = new MerchantDetailsFragment(merchant);
        ListFragment list4= new ListFragment(Merchantid,"联盟套餐");
        ListFragment list5= new ListFragment(Merchantid,"联盟预存");
        fragments.add(list3);
        fragments.add(list1);
        fragments.add(list4);
        fragments.add(list5);
        fragments.add(list2);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        pst.setViewPager(viewPager);
        viewPager.setCurrentItem(0);

        // 设置Tab是自动填充满屏幕的
        pst.setShouldExpand(true);
// 设置Tab的分割线的颜色
        pst.setDividerColor(getResources().getColor(R.color.colorred));
// 设置分割线的上下的间距,传入的是dp
        pst.setDividerPaddingTopBottom(10);
////设置Tab底部线的颜色
        pst.setUnderlineColor(getResources().getColor(R.color.bg_line));

// 设置Tab 指示器Indicator的高度,传入的是dp
        pst.setIndicatorHeight(3);
// 设置Tab Indicator的颜色
        pst.setIndicatorColor(getResources().getColor(R.color.colorred));

// 设置Tab标题文字的大小,传入的是sp
        pst.setTextSize(13);
// 设置选中Tab文字的颜色
        pst.setSelectedTextColor(getResources().getColor(R.color.colorred));
//设置正常Tab文字的颜色
        pst.setTextColor(getResources().getColor(R.color.colorblack));
//设置Tab文字的左右间距,传入的是dp
        pst.setTabPaddingLeftRight(6);

//是否支持动画渐变(颜色渐变和文字大小渐变)
        pst.setFadeEnabled(false);
// 设置最大缩放,是正常状态的0.3倍
//        pst.setZoomMax(0.3F);
        pst.setZoomMax(0);

//这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
        pst.setSmoothScrollWhenClickTab(true);
    }

    /**
     * * 自定义适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }//设置每页的标题

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        //设置每一页对应的fragment
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        //fragment的数量
        @Override
        public int getCount() {
            return fragments.size();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                delAct(mContent);
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            delAct(mContent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
